<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;
/**
 * @mixin \think\Model
 */
class Admin extends Model
{
    //软删除设置
    protected $table="admin";
    use SoftDelete;
    protected $deleteTime = 'delete_time';
//列表展示和搜索
    public function list($keyword)
    {
        return $this->where('name','like',"%$keyword%")->paginate([
            'list_rows'=>10,
            'query'=>compact('keyword')
        ]);
    }
//添加功能
    public function addUse($data)
    {
        $list = $this->where('name',$data['name'])->select();
        if ($list)
        {
            return false;
        }
        return $this->insert($data);
    }
//单删功能
    public function delUse($id)
    {
        $data = $this->find($id);
        if ($data['state']=='正常')
        {
            return true;
        }
        return $this->destroy($id);
    }
//批删功能
    public function delAll($id)
    {
        foreach ($id as $value)
        {
            $data = $this->find($value);
            if ($data['state']!='正常'){
                $this->destroy($id);
            }
        }
        return true;
    }
//查找功能
    public function findUse($id)
    {
        return $this->find($id);
    }
//修改功能
    public function updateUse($data)
    {
//        dd($data['name']);
        $list = $this->find($data['id']);

        $list->name = $data['name'];
        $list->control = $data['control'];
        $list->state = $data['state'];
        return $list->save();
    }
}
